<template>
	<view>
		<view>
			<navTop title="Create" :isHeight="false" hasBgc="white" :color="navtop.color" :isBack="true"></navTop>
		</view>

		<form>

			<view>
				<image src="https://img01-xusong.91q.com/B3F73674-9276-410E-80D5-FEE133BD6AAE.png" class="formIcon1">
				</image>
				<input class="form1" v-model="associationData.name" focus placeholder="社团名"
					placeholder-style="color:#C6C7CC" />
			</view>

			<view>
				<image src="https://img01-xusong.91q.com/F7E65629-58F2-4344-AE64-78D5A18751C6.png" class="formIcon2">
				</image>
				<input class="form2" v-model="associationData.proprieter" focus placeholder="人数限制"
					placeholder-style="color:#C6C7CC" />
			</view>

			<view>
				<image src="https://img01-xusong.91q.com/3637C938-52DC-48E4-96D3-930B3AA506C7.png" class="formIcon3">
				</image>
				<input class="form3" v-model="associationData.proprieter_phone" focus placeholder="社团主题"
					placeholder-style="color:#C6C7CC" />
			</view>

			<view>
				<image src="https://img01-xusong.91q.com/A5B2DF20-1911-4BF2-81FF-89CFAAA3AADB.png" class="formIcon4">
				</image>
				<textarea class="form4" v-model="associationData.introduce" placeholder="社团介绍"
					placeholder-style="color:#C6C7CC"></textarea>
			</view>

			<image src="https://img01-xusong.91q.com/B0ABBC36-A8A0-4FB4-BAE5-A5059A6C93A6.png" class="formIcon5">
			</image>
			<view class="form5">
				<u--form>
					<u-form-item prop="associationData.type" borderBottom @click="showSex = true; hideKeyboard()"
						ref="item1">
						<u--input v-model="associationData.type" disabled disabledColor="#ffffff" placeholder="社团类型"
							border="none"></u--input>
						<u-icon slot="right" name="arrow-right"></u-icon>
					</u-form-item>
					<u-action-sheet :show="showSex" :actions="actions" title="请选择社团类型" @close="showSex = false"
						@select="sexSelect">
					</u-action-sheet>
				</u--form>
			</view>
			<view class="logoText">社团LOGO</view>
			<view class="logo">
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="1"></u-upload>
			</view>
		</form>

		<!-- <button class="confirm" @click="uploadFilePromise(url),uploadMessage()">提交</button> -->
		<button class="confirm" @click="confirm">提交</button>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import navTop from '../../components/navTop.vue'
	export default {
		components: {
			navTop
		},
		data() {
			return {
				user_Id: this.$store.state.userInfo.id,
				message: '申请创建社团成功，请等待审核',
				navtop: {
					title: 'XXX',
					color: '#000000'
				},

				actions: [{
						name: '学生部门',
					},
					{
						name: '学生社团',
					},
					{
						name: '其他'
					},

				],
				showSex: false,
				fileList1: [],
				associationData: {
					name: '',
					proprieter: '',
					proprieter_phone: '',
					introduce: '',
					type: '',
					userId: this.$store.state.userInfo.id
				}
			}
		},
		methods: {
			confirm() {
				let that = this
				let data = this.associationData
				uni.showModal({

					title: '确定要创建该社团吗？',
					content: '确定后请耐心等待审核',
					success: function(res) {
						if (res.confirm) {
							if ((data.name != "") && (data.proprieter != "") &&
								(data.proprieter_phone != "") && (data.introduce != "") &&
								(data.type != "")) {
								that.uploadFilePromise();
								that.uploadMessage();
								uni.navigateBack({
									delta: 1,
								})
							} else {
								uni.showModal({
									content: '请输入完整的社团信息'
								})
							}
						} else if (res.cancel) {

						}
					}
				});
			},
			//发送消息
			uploadMessage() {
				let userId = this.user_Id
				let msg = this.message
				uni.request({
					url: 'https://www.codingyt.com/club/message/insert', //仅为示例，并非真实接口地址。
					method: 'POST',
					dataType: 'json',

					data: {
						msg,
						userId
					},
					success: (res) => {
						uni.navigateTo({
							url: '/pages/index/index',
						});

					}
				});

			},
			hideKeyboard() {
				uni.hideKeyboard()
			},
			sexSelect(e) {
				this.associationData.type = e.name
			},
			//图片上传提交
			uploadFilePromise(url) {
				let data = this.associationData
				return new Promise((resolve, reject) => {
					uni.compressImage({
						src: url,
						quality: 10,
						success: res => {
							// console.log(res.tempFilePath)
							let a = uni.uploadFile({
								url: 'https://www.codingyt.com/club/organization/add ', // 仅为示例，非真实的接口地址
								filePath: res.tempFilePath,
								name: 'img',
								formData: data,
								success: (res) => {
									setTimeout(() => {
										resolve(res.data.data)

									}, 1000)

								},
							});
						}
					})

				})


			},
			//页面返回
			pageBack() {
				uni.navigateBack({
					delta: 1
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
		},

	}
</script>

<style scoped>
	.formIcon1 {
		left: 22rpx;
		top: 206rpx;
		width: 72rpx;
		height: 72rpx;
		position: absolute;
		z-index: 50;
	}

	.form1 {
		left: 104rpx;
		top: 198rpx;
		width: 598rpx;
		height: 84rpx;
		border-radius: 40rpx;
		color: rgba(136, 136, 136, 100);
		font-size: 28rpx;
		padding-left: 24rpx;
		box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(0, 0, 0, 0.4);
		font-family: Microsoft Yahei;
		border: 2rpx solid rgba(245, 166, 35, 100);
		position: absolute;
	}

	.formIcon2 {
		left: 22rpx;
		top: 338rpx;
		width: 64rpx;
		height: 64rpx;
		position: absolute;
	}

	.form2 {
		left: 104rpx;
		top: 326rpx;
		width: 196rpx;
		height: 88rpx;
		padding-left: 24rpx;
		border-radius: 40rpx;
		color: rgba(136, 136, 136, 100);
		font-size: 28rpx;
		text-align: left;
		box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(0, 0, 0, 0.4);
		font-family: Microsoft Yahei;
		border: 2rpx solid rgba(245, 166, 35, 100);
		position: absolute;
	}

	.formIcon3 {
		left: 20rpx;
		top: 476rpx;
		width: 70rpx;
		height: 70rpx;
		position: absolute;
	}

	.form3 {
		left: 108rpx;
		top: 466rpx;
		width: 598rpx;
		height: 84rpx;
		padding-left: 24rpx;
		border-radius: 40rpx;
		color: rgba(136, 136, 136, 100);
		font-size: 28rpx;
		text-align: left;
		box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(0, 0, 0, 0.4);
		font-family: Microsoft Yahei;
		border: 2rpx solid rgba(245, 166, 35, 100);
		position: absolute;
	}

	.formIcon4 {
		left: 22rpx;
		top: 678rpx;
		width: 70rpx;
		height: 70rpx;
		position: absolute;
	}

	.form4 {
		left: 104rpx;
		top: 610rpx;
		width: 592rpx;
		height: 200rpx;
		padding-left: 24rpx;
		padding-top: 20rpx;
		line-height: 40rpx;
		border-radius: 50rpx;
		color: rgba(136, 136, 136, 100);
		font-size: 28rpx;
		text-align: left;
		box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(0, 0, 0, 0.4);
		font-family: Microsoft Yahei;
		border: 2rpx solid rgba(245, 166, 35, 100);
		position: absolute;
	}

	.formIcon5 {
		left: 362rpx;
		top: 338rpx;
		width: 70rpx;
		height: 70rpx;
		position: absolute;
	}

	.form5 {
		left: 450rpx;
		top: 328rpx;
		width: 204rpx;
		height: 72rpx;
		line-height: 40rpx;
		color: #8C8C8C;
		font-size: 28rpx;
		padding-left: 20rpx;
		padding-bottom: 12rpx;
		box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(0, 0, 0, 0.4);
		font-family: Microsoft Yahei;
		border: 2rpx solid rgba(245, 166, 35, 100);
		border-radius: 40rpx;
		position: absolute;
	}

	.logoText {
		left: 30rpx;
		top: 880rpx;
		width: 380rpx;
		height: 50rpx;
		font-size: 40rpx;
		font-weight: 550;
		position: absolute;

	}

	.logo {
		left: 50rpx;
		top: 960rpx;
		position: absolute;
		z-index: 9;
	}

	.confirm {
		left: 10rpx;
		top: 1200rpx;
		width: 460rpx;
		height: 110rpx;
		line-height: 110rpx;
		border-radius: 24rpx;
		background-color: #F5A623;
		color: white;
		font-size: 40rpx;
		text-align: center;
		box-shadow: 0rpx 2rpx 12rpx 0rpx rgba(0, 0, 0, 0.4);
		font-family: Arial;
	}
</style>
